package icu.zqyhdm.blog.controller;

import icu.zqyhdm.blog.constant.RedisConst;
import icu.zqyhdm.blog.vo.*;
import icu.zqyhdm.result.Result;
import icu.zqyhdm.blog.service.UserService;
import icu.zqyhdm.helper.JwtHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;
import java.util.Set;


/**
 * @Description
 * @Author lifu
 * @Date 2021/6/22 21:56
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping(value = "/api/blog/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "所有用户列表带分页")
    @GetMapping(value = "/listAll")
    public Result List(){
        return Result.ok(userService.list());
    }

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    public Result add(@Valid @RequestBody UserRegisterVo userRegisterVo){
        userService.register(userRegisterVo);
        return Result.ok();
    }

    @ApiOperation(value = "手机号验证码登录")
    @PostMapping(value = "/login/phone")
    public Result login(@Valid @RequestBody UserPhoneLoginVo userPhoneLoginVo){
        String token = userService.phoneLogin(userPhoneLoginVo);
        return Result.ok(token);
    }

    @ApiOperation(value = "账号密码登录")
    @PostMapping(value = "/login/account")
    public Result login(@Valid @RequestBody UserAccountLoginVo userPhoneLoginVo, HttpServletRequest request){
        String token = userService.accountLogin(userPhoneLoginVo, request);
        return Result.ok(token);
    }

    @ApiOperation(value = "后台登录")
    @PostMapping(value = "/adminLogin/account")
    public Result adminLogin(@Valid @RequestBody AdminLoginVo adminLoginVoLoginVo){
        Map<String, String> map = userService.adminLogin(adminLoginVoLoginVo);
        return Result.ok(map);
    }

    @ApiOperation(value = "获取登录用户信息")
    @GetMapping(value = "/info")
    public Result getUserDetail(@RequestParam("token") String token){
        Integer userId = JwtHelper.getUserId(token);
        // 先从缓存查mysql的基本静态数据
        UserDetailVo userDetail = userService.getUserDetail(userId);
        // 从再redis查浏览数、点赞数动态数据
        userDetail.setArticleLikeSet(redisTemplate.opsForSet().members(RedisConst.ARTICLE_USER_LIKE + userId.toString()));
        userDetail.setCommentLikeSet(redisTemplate.opsForSet().members(RedisConst.COMMENT_USER_LIKE + userId.toString()));
        return Result.ok(userDetail);
    }

    //查询用户详情信息
    @ApiOperation(value = "查询用户详情信息")
    @GetMapping(value = "/info/{userId}")
    public Result getUserDetail(@PathVariable("userId") Integer userId) {
        UserDetailVo userDetail = userService.getUserDetail(userId);
        return Result.ok(userDetail);
    }

    @ApiOperation(value = "登出")
    @PostMapping(value = "/logout")
    public Result logout(@RequestHeader("X-Token") String token){
        //移除session中的登录标记
        return Result.ok();
    }

    //发送手机验证码
    @ApiOperation(value = "发送手机验证码")
    @GetMapping(value = "/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone) {
        userService.send(phone);
        return Result.ok();
    }

}
